using MediatR;
using Medical.Api.Read.Application.Command.Wms.WmsStoresCommand;
using Medical.Api.Read.Dto.WmsDto.WmsStoresDro;
using Medical.Domain.BasicManagement;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;

namespace Medical.Api.Read.Application.Handler.Wms.WmsStoresHandler
{
    /// <summary>
    /// 获取货架详情处理器
    /// </summary>
    public class GetShelvesDetailByIdHandler : IRequestHandler<GetShelvesDetailByIdCommand, ApiResult<ShelvesDetailDto>>
    {
        private readonly IBaseRepository<Shelves> _shelvesRepository;
        private readonly ILogger<GetShelvesDetailByIdHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="shelvesRepository">货架仓储</param>
        /// <param name="logger">日志记录器</param>
        public GetShelvesDetailByIdHandler(
            IBaseRepository<Shelves> shelvesRepository,
            ILogger<GetShelvesDetailByIdHandler> logger)
        {
            _shelvesRepository = shelvesRepository;
            _logger = logger;
        }

        /// <summary>
        /// 处理获取货架详情请求
        /// </summary>
        /// <param name="request">请求命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>货架详情</returns>
        public async Task<ApiResult<ShelvesDetailDto>> Handle(GetShelvesDetailByIdCommand request, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogInformation("开始获取货架详情，ID: {Id}", request.Id);

                // 获取货架信息
                var shelves = _shelvesRepository.GetModel(request.Id);
                if (shelves == null || shelves.IsDeleted)
                {
                    return new ApiResult<ShelvesDetailDto>
                    {
                        Code = ApiEnums.NotFound,
                        Msg = "货架不存在",
                        Data = null
                    };
                }

                // 获取上级货架名称
                string parentShelfName = string.Empty;
                if (shelves.ParentShelfId.HasValue)
                {
                    var parentShelf = _shelvesRepository.GetModel(shelves.ParentShelfId.Value);
                    parentShelfName = parentShelf?.ShelfName ?? string.Empty;
                }

                // 构建返回DTO
                var result = new ShelvesDetailDto
                {
                    Id = shelves.Id,
                    ShelfCode = shelves.ShelfCode,
                    ShelfName = shelves.ShelfName,
                    ParentShelfId = shelves.ParentShelfId,
                    ParentShelfName = parentShelfName,
                    Barcode = shelves.Barcode,
                    Layers = shelves.Layers,
                    Grids = shelves.Grids,
                    Status = shelves.Status,
                    StatusName = shelves.Status == 1 ? "启用" : "禁用",
                    CreateTime = shelves.CreateTime,
                    CreateUserName = shelves.CreateUserName
                };

                _logger.LogInformation("成功获取货架详情，ID: {Id}", request.Id);

                return new ApiResult<ShelvesDetailDto>
                {
                    Code = ApiEnums.Success,
                    Msg = "获取成功",
                    Data = result
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取货架详情时发生异常，ID: {Id}", request.Id);
                return new ApiResult<ShelvesDetailDto>
                {
                    Code = ApiEnums.Fail,
                    Msg = "获取失败：系统内部错误",
                    Data = null
                };
            }
        }
    }
}